using Godot;
using System;

public partial class 钢体物理系统操作 : RigidBody2D
{

    public float speed = 200f;


    public AnimatedSprite2D body;

    public float JumpCount = 0;

    public bool IsOnGround = false;

    public override void _Ready()
    {
        LockRotation = true;
        PhysicsMaterialOverride.Bounce = 0;
        body = GetNode<AnimatedSprite2D>("AnimatedSprite2D");

    }

    public override void _PhysicsProcess(double delta)
    {
        var animated = false;

        var f = Input.GetAxis("左", "右");
        if (f < 0)
        {
            animated = true;
            body.FlipH = true;

        }
        if (f > 0)
        {
            animated = true;
            body.FlipH = false;
        }
        LinearVelocity = new Vector2(f * speed, LinearVelocity.Y);
        if (IsOnGround || JumpCount <= 2)
        {
            if (Input.IsActionPressed("空格"))
            {
                GD.Print(JumpCount);

                LinearVelocity = new Vector2(f * speed, -1.5f * speed);
            }
        }
        if (animated)
        {
            body.Play("player");
        }
        if (!animated)
        {
            body.Stop();
        }
    }

    public void OnBodyEnterd(Node node)
    {
        JumpCount = 0;
        IsOnGround = true;
    }

    public void OnBodyExit(Node node)
    {
        IsOnGround = false;
    }

    public override void _Input(InputEvent @event)
    {
        if (@event is InputEventKey keyEvent)
        {
            if (keyEvent.Keycode == Key.Space && keyEvent.IsPressed())
            {
                JumpCount += 1;
            }

        }
    }

}
